﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Workflow.Interfaces;
using Microhard.Core.DBUtility;

namespace Microhard.JHWorkflow
{
    public class JHWorkflowImpl : IWorkflow
    {

        public Core.DBUtility.ExecutedResult Add(WorkflowModel model,)
        {
            ExecutedResult result = new ExecutedResult();
            //获得基础信息
            var info = JHWorkflowTool.GetFlowInfo(model);
            var biaoshi = JHWorkflowTool.GetDJBiaoshi(info.DjSN);
            var bsid = JHWorkflowTool.UpdateDJBiaoshi_Recnum(biaoshi);
            //插入业务数据
            C6Entities db = new C6Entities();
            db.MH_Projects.AddObject(new MH_Projects
            {
                MainID = bsid,
                PrjName = "test" + DateTime.Now.ToString()
            });
            db.SaveChanges();
            var infowflow = JHWorkflowTool.GetTemplate(info.AppID, info.DjSN);
            //发起流程
            JHSoft.WorkFlow.BLL.Approve.ApproveDeal deal = new JHSoft.WorkFlow.BLL.Approve.ApproveDeal();
            int intStartAppDID = deal.getFlowStartStepDID(info.AppID, string.Empty, infowflow.Version);
            string InstanceID = System.Guid.NewGuid().ToString().Replace("-", "");
            string userName = JHWorkflowTool.GetUserName(info.UserCode);
            int AppID;
            if (JHWorkflowTool.CreateFlowInstance(info.AppID, InstanceID, string.Empty, userName, infowflow.Version))
            {
                string strCurDate = DateTime.Now.ToString("yyyy-MM-dd");
                string strAppOTitle = userName + "的" + infowflow.FlowTypeName;
                AppID = JHWorkflowTool.InsertFlowFirstStep(info.UserCode, info.AppID, intStartAppDID, 0, bsid, strAppOTitle, 0, strCurDate, strCurDate, 0, InstanceID);
                string strUrl = JHWorkflowTool.GetFormInstance(intStartAppDID, InstanceID);
                if (strUrl != "")
                {
                    strUrl = strUrl.Replace("&httpAppID=-1", string.Empty);
                    strUrl = strUrl.Replace("IsNew=0", "IsNew=1");
                    strUrl = strUrl.Replace("opentype=1", "opentype=2");
                }
                JHWorkflowTool.SaveFormInstance(InstanceID, string.Empty, strUrl, intStartAppDID);
                var intExecResult = JHWorkflowTool.StorageForTheMoment(info.AppID, AppID, 1, "", info.UserCode, info.UserCode, "0", "0", "", strAppOTitle, InstanceID, 0, bsid);
                if (intExecResult != 1)
                {
                    result.ErrorMsg.Add("JHWorkflowImpl.Add", "暂存流程失败");
                }
            }
            else
            {
                result.ErrorMsg.Add("JHWorkflowImpl.Add", "创建流程实例失败");
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Next(WorkflowModel model)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ExecutedResult Rollback(WorkflowModel model)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ExecutedResult End(WorkflowModel model)
        {
            throw new NotImplementedException();
        }

        public string SendNotification(WorkflowModel model)
        {
            throw new NotImplementedException();
        }
    }
}
